package com.newviews.tcmip.common.model;


import com.newviews.tcmip.common.constant.CommonConsts;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一的接口返回数据格式
 *
 * @author ChenLong
 */
@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class ResultModel<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    @ApiModelProperty(value = "成功标志")
    private boolean success = true;

    /**
     * 返回处理消息
     */
    @ApiModelProperty(value = "返回处理消息")
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T data;

    /**
     * 时间戳
     */
    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();


    public ResultModel() {
    }

    public ResultModel(T data) {
        this.message = "操作成功";
        this.code = CommonConsts.SC_OK_200;
        this.success = true;
        this.data = data;
    }

    public ResultModel(boolean success, String message, Integer code, T data) {
        this.message = message;
        this.code = code;
        this.success = success;
        this.data = data;
    }


    /**
     * 成功
     *
     * @return
     */
    public ResultModel<T> success() {
        this.message = "操作成功";
        this.code = CommonConsts.SC_OK_200;
        this.success = true;
        return this;
    }

    /**
     * 成功并返回数据
     *
     * @param data
     * @return
     */
    public ResultModel<T> success(T data) {
        this.message = "操作成功";
        this.code = CommonConsts.SC_OK_200;
        this.success = true;
        this.data = data;
        return this;
    }

    /**
     * 成功并返回消息和对象
     *
     * @param message
     * @param data
     * @return
     */
    public ResultModel<T> success(String message, T data) {
        this.message = message;
        this.code = CommonConsts.SC_OK_200;
        this.success = true;
        this.data = data;
        return this;
    }

    /**
     * 失败并返回错误信息
     *
     * @param message
     * @return
     */
    public ResultModel<T> failure(String message) {
        this.message = message;
        this.code = CommonConsts.SC_INTERNAL_SERVER_ERROR_500;
        this.success = false;
        return this;
    }

    /**
     * 失败,并返回自定义异常
     *
     * @param code
     * @param message
     * @return
     */
    public ResultModel<T> failure(Integer code, String message) {
        this.message = message;
        this.code = code;
        this.success = false;
        return this;
    }

    public boolean isSuccess() {
        return this.success;
    }
}